package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.WorklinesectionMapper;
import com.lucher.mes.dao.WorklinesectionmachineMapper;
import com.lucher.mes.dao.WorksectionMapper;
import com.lucher.mes.po.Worksection;

@Controller
@RequestMapping("/worksection")  
public class WorksectionController extends BaseController{
	@Autowired
	private WorksectionMapper worksectionDao;
	@Autowired
	private WorklinesectionMapper worklinesectionDao;
	@Autowired
	private WorklinesectionmachineMapper worklinesectionmachineDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Worksection form) {
		return "worksection/findWorksectionList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Worksection form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = worksectionDao.findByCount(form);
		List<Worksection> list = worksectionDao.findByListForPage(form);
		
		for (Worksection worksection : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", worksection.getId());
			objectMap.put("code", worksection.getCode());
			objectMap.put("name", worksection.getName());
			objectMap.put("info", worksection.getInfo());
			objectMap.put("remark", worksection.getRemark());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Worksection form) {
		return "worksection/addWorksection";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Worksection form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getCode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入编号");
			return responseJson;
		} else {
			Worksection oldWorksection = worksectionDao.findByCode(form.getCode());
			if (oldWorksection != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此编号已经存在");
				return responseJson;
			}
		}
		
		//保存工位机信息
		Integer worksectionId = worksectionDao.insertSelective(form);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Worksection form) {
		Worksection worksection = worksectionDao.selectByPrimaryKey(form.getId());
		form.setWorksection(worksection);

		return "worksection/updateWorksection";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Worksection form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getCode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，工位机编号不能为空");
			return responseJson;
		} else {
			Worksection oldWorksection = worksectionDao.findByCode(form.getCode());
			if (oldWorksection != null && !oldWorksection.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，工位机编号已经存在");
				return responseJson;
			}
		}

		// 修改信息
		int flag = worksectionDao.updateByPrimaryKeySelective(form);
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	@Transactional
	public Map<String,Object> delete(Worksection form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		//查找需要删除的工段
		Worksection demoDel = worksectionDao.selectByPrimaryKey(form.getId());
		//删除工段
		worksectionDao.deleteByPrimaryKey(form.getId());
		//删除工段与产线的关系
		worklinesectionDao.deleteBySectioncode(demoDel.getCode());
		//删除工段与工位机的关系
		worklinesectionmachineDao.deleteBySectioncode(demoDel.getCode());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Worksection> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Worksection worksection : formlist) {
			//删除工位机
			worksectionDao.deleteByPrimaryKey(worksection.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
}
